package com.ganbo.dh.controller;


import com.netsdk.demo.module.LoginModule;
import com.netsdk.lib.NetSDKLib;
import com.sun.jna.Pointer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;

@Controller
public class IndexController {

    // device disconnect callback instance
    private static DisConnect disConnect = new DisConnect();

    // device reconnect callback instance
    private static HaveReConnect haveReConnect = new HaveReConnect();


    private String fail = "fail";

    @GetMapping(value = "/index")
    public String index(Model model) {
        model.addAttribute("haha", "我的天");
        return "/index";
    }


    @GetMapping("/test")
    @ResponseBody
    public String test() {
        System.out.println("web项目设置...");

        //①：初始化设备
        boolean initResult = LoginModule.init(disConnect, haveReConnect);// init sdk
        if (initResult) {
            System.out.println("初始化设备成功");
        } else {
            System.out.println("初始化设备失败");
            return fail;
        }


        //②：登陆设备
        boolean loginResult = LoginModule.login("192.168.1.143", 37777, "admin", "dahua123456,.");
        if (loginResult) {
            System.out.println("登陆成功");
        } else {
            System.out.println("登陆失败");
            return fail;
        }

        return "success";
    }

    /////////////////function///////////////////
    // device disconnect callback class
    // set it's instance by call CLIENT_Init, when device disconnect sdk will call it.
    private static class DisConnect implements NetSDKLib.fDisConnect {
        public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
            System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    System.out.println("初始化回调函数11111。。。。。");
                }
            });
        }
    }


    // device reconnect(success) callback class
    // set it's instance by call CLIENT_SetAutoReconnect, when device reconnect success sdk will call it.
    private static class HaveReConnect implements NetSDKLib.fHaveReConnect {
        public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
            System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    System.out.println("初始化回调函数2222222222.。。");
                }
            });
        }
    }


}
